package com.photologics.logic;

import android.app.Dialog;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.photologics.logic.LogicHelper.item;
import com.photologics.main.R;
import com.photologics.main.ShopDialog;

public class ItemUseDialog extends Dialog implements OnClickListener {
	private final String TAG = "ItemUseDialog";
	private LinearLayout verticalItem, horizontalItem, squarItem, showItem,
			removeItem, shopIcon;
	private TextView verticalItemCount, horizontalItemCount, squarItemCount,
			showItemCount, removeItemCount;
	private Context con;

	public ItemUseDialog(Context context) {
		super(context);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.item_use_dialog);
		con = context;
		
		verticalItem = (LinearLayout) findViewById(R.id.vertical_item);
		horizontalItem = (LinearLayout) findViewById(R.id.horizontal_item);
		squarItem = (LinearLayout) findViewById(R.id.squar_item);
		showItem = (LinearLayout) findViewById(R.id.show_item);
		removeItem = (LinearLayout) findViewById(R.id.remove_item);
		shopIcon = (LinearLayout) findViewById(R.id.shop_icon);

		verticalItem.setOnClickListener(this);
		horizontalItem.setOnClickListener(this);
		squarItem.setOnClickListener(this);
		removeItem.setOnClickListener(this);
		showItem.setOnClickListener(this);
		shopIcon.setOnClickListener(this);

		verticalItemCount = (TextView) findViewById(R.id.vertical_item_count);
		horizontalItemCount = (TextView) findViewById(R.id.horizontal_item_count);
		squarItemCount = (TextView) findViewById(R.id.squar_item_count);
		showItemCount = (TextView) findViewById(R.id.show_item_count);
		removeItemCount = (TextView) findViewById(R.id.remove_item_count);

		verticalItemCount.setText(Integer.toString(LogicHelper.accountInfo
				.getVertical()) + "개");
		horizontalItemCount.setText(Integer.toString(LogicHelper.accountInfo
				.getHorizontal()) + "개");
		squarItemCount.setText(Integer.toString(LogicHelper.accountInfo
				.getSquar()) + "개");
		showItemCount.setText(Integer.toString(LogicHelper.accountInfo
				.getShowImage()) + "개");
		removeItemCount.setText(Integer.toString(LogicHelper.accountInfo
				.getRemoveX()) + "개");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.vertical_item:
			if (LogicHelper.accountInfo.getVertical() > 0) {
				Log.i(TAG, "use vertical item");
				LogicActivity.setUseItem(item.VERTICAL);
				dismiss();
			} else
				Toast.makeText(con, "아이템이 없습니다",
						Toast.LENGTH_SHORT).show();
			break;
		case R.id.horizontal_item:
			if (LogicHelper.accountInfo.getHorizontal() > 0) {
				Log.i(TAG, "use horizontal item");
				LogicActivity.setUseItem(item.HORIZONTAL);
				dismiss();
			} else
				Toast.makeText(con, "아이템이 없습니다",
						Toast.LENGTH_SHORT).show();
			break;
		case R.id.squar_item:
			if (LogicHelper.accountInfo.getSquar() > 0) {
				Log.i(TAG, "use squar item");
				LogicActivity.setUseItem(item.SQUAR);
				dismiss();
			} else
				Toast.makeText(con, "아이템이 없습니다",
						Toast.LENGTH_SHORT).show();
			break;
		case R.id.show_item:
			if (LogicHelper.accountInfo.getShowImage() > 0) {
				Log.i(TAG, "use show item");
				LogicActivity.setUseItem(item.SHOW);
				dismiss();
			} else
				Toast.makeText(con, "아이템이 없습니다",
						Toast.LENGTH_SHORT).show();
			break;
		case R.id.remove_item:
			if (LogicActivity.getFailCount() != 0 && LogicHelper.accountInfo.getRemoveX() > 0) {
				Log.i(TAG, "use remove item");
				LogicActivity.setUseItem(item.REMOVE);
				dismiss();
			} else{
				if(LogicActivity.getFailCount() == 0)
					Toast.makeText(con, "틀린게 없습니다.",
							Toast.LENGTH_SHORT).show();
				else if(LogicHelper.accountInfo.getRemoveX() <= 0)
					Toast.makeText(con, "아이템이 없습니다.",
							Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.shop_icon:
			ShopDialog shopdialog = new ShopDialog(con);
			shopdialog.show();
			dismiss();
		}
	}
}
